package com.ijiuchuang.jc_localization.presenter.settings;

import android.content.Context;
import android.graphics.Bitmap;

import com.ijiuchuang.jc_localization.bean.lite.Map;
import com.ijiuchuang.jc_localization.bean.lite.Station;
import com.ijiuchuang.jc_localization.contract.settings.SettsMapDetailContract;
import com.ijiuchuang.jc_localization.model.settings.SettsMapDetailModel;
import com.ijiuchuang.jc_localization.tools.utils.Log;

import java.util.ArrayList;
import java.util.List;

import rx.Subscriber;
import rx.functions.Action1;

/**
 * Created by Veev on 2016/6/20
 *
 * @link 384506557@qq.com
 */
public class SettsMapDetailPresenter implements SettsMapDetailContract.Presenter {

    private Context mContext;
    private SettsMapDetailContract.View mView;
    private SettsMapDetailContract.Model mModel;

    public SettsMapDetailPresenter(Context mContext, SettsMapDetailContract.View mView) {
        this.mContext = mContext;
        this.mView = mView;
        mModel = new SettsMapDetailModel();

        mView.setPresenter(this);
    }

    @Override
    public void start() {
        // start here
    }

    @Override
    public void getMapBitmap(int position) {
        mModel.getMapBitmap(mContext, position, new Action1<Bitmap>() {
            @Override
            public void call(Bitmap bitmap) {
                mView.setMap(bitmap);
                mView.setSize("Width: " + bitmap.getWidth() + ", Height: " + bitmap.getHeight());
            }
        });
    }

    @Override
    public void getSelectStation(int position) {
        mModel.getStation(mView.getMap(), position, new Subscriber<Station>() {
            @Override
            public void onCompleted() {
            }

            @Override
            public void onError(Throwable throwable) {
                Log.i("Error on SettsMapDetailPresenter getSelectStation: " + throwable.toString());
            }

            @Override
            public void onNext(Station station) {
                mView.showUpdateStation(station);
            }
        });
    }

    @Override
    public void loadConfig(final Map map) {
        mModel.getMapsList(mContext, new Action1<String[]>() {
            @Override
            public void call(String[] strings) {
                mView.setDatas(strings);
                if (map == null) {
                    mView.setSpinner(0);
                    //mView.setName("编辑名称");
                    mView.setSubtitle("新情景");
                    mView.setDefaultLinearVisibility(false);
                } else {
                    mView.setSpinner(map.getMapSpinner());
                    //mView.setName(map.getName());
                    mView.setSubtitle(map.getName());
                }
            }
        });

        if (map != null) {

            mModel.getDefaultMap(new Subscriber<Number>() {
                @Override
                public void onCompleted() {
                    mView.setSwitchChecked(true);
                }

                @Override
                public void onError(Throwable e) {
                    mView.setSwitchChecked(true);
                }

                @Override
                public void onNext(Number number) {
                    if (number.longValue() == map.getId()) {
                        mView.setSwitchChecked(true);
                    } else {
                        mView.setSwitchChecked(false);
                    }
                }
            });

            mModel.loadStation(map, new Subscriber<List<Station>>() {
                @Override
                public void onCompleted() {
                }

                @Override
                public void onError(Throwable e) {
                }

                @Override
                public void onNext(List<Station> stations) {
                    if (!stations.isEmpty()) {
                        List<String> list = new ArrayList<>();
                        for (Station s : stations) {
                            mView.addStations(s);
                            list.add(s.getNum() + "号基站 X: " + s.getX() + ",Y: " + s.getY());
                        }
                        mView.setStationSpinner(list);
                    }
                }
            });
        }
    }

    @Override
    public void saveMap(Map map, String name, int spinner) {
        mModel.saveMap(mContext, map, name, spinner);
    }

    @Override
    public void setDefault(Map map) {
        mModel.setDefault(map);
    }

    @Override
    public void getAnimation() {
        switch (mModel.getAnimation()) {
            case SettsMapDetailModel.ANIMATION_FULLSCREEN:
                mView.animationFull();
                break;
            case SettsMapDetailModel.ANIMATION_FULLSCREEN_EXIT:
                mView.animationFullExit();
                break;
        }
    }

    @Override
    public int getTopHeight() {
        return mModel.getTopHeight();
    }

    @Override
    public void setTopHeight(int height) {
        mModel.setTopHeight(height);
    }

    @Override
    public boolean inputVerify(String num, String mac, String x, String y, String limit, String remark, boolean isShown, Map map) {
        if (mModel.inputVerify(num, mac, x, y, limit, remark, isShown, map)) {
            saveStation(num, mac, x, y, limit, remark, isShown, map);
            return true;
        } else {
            return false;
        }
    }

    @Override
    public boolean updateVerify(Station station, String num, String mac, String x, String y, String limit, String remark, boolean isShown, Map map) {
        if (mModel.inputVerify(num, mac, x, y, limit, remark, isShown, map)) {
            updateStation(station, num, mac, x, y, limit, remark, isShown, map);
            return true;
        } else {
            return false;
        }
    }

    @Override
    public void saveStation(String num, String mac, String x, String y, String limit, String remark, boolean isShown, Map map) {
        mView.addStations(mModel.saveStation(num, mac, x, y, limit, remark, isShown, map));
    }

    @Override
    public void updateStation(Station station, String num, String mac, String x, String y, String limit, String remark, boolean isShown, Map map) {
        mModel.updateStation(station, num, mac, x, y, limit, remark, isShown, map);
    }

}